using QFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace QFramework
{
    public class ItemKit
    {
        public static Dictionary<string, SlotGroup> DictSlotGroup = new();
        public static Dictionary<string, IItem> DictItem = new();
        public static IItemKitSaveHandler SaverHandler = new DefaultItemKitSaveHandler();
        public static IItemKitResLoadHandler ResLoadHandler = new DefaultItemKitResLoadHandler();
        public static UISlot CurrentSlotPointerOn = null;
        public const string DefaultLanguage = "DefaultLanguage";
        public static string CurrentLanguage { get; private set; } = DefaultLanguage;
        public static SlotGroup CreateSlotGroup(string key)
        {
            var slotGroup = new SlotGroup(key);
            DictSlotGroup.Add(key, slotGroup);
            return slotGroup;
        }
        public static SlotGroup GetSlotGroupByKey(string key)
        {
            return DictSlotGroup[key];
        }

        public static void AddItemConfig(IItem itemConfig)
        {
            DictItem.Add(itemConfig.GetKey, itemConfig);
        }

        #region ResLoad
        public static void LoadItemDatabase(string databaseName)
        {
            var database = ResLoadHandler.LoadItemDatabase(databaseName);
            foreach (var item in database.Items)
            {
                AddItemConfig(item);
            }
        }
        public static void LoadLanguagePackage(string languagePackageName)
        {
            if (languagePackageName == DefaultLanguage)
            {
                CurrentLanguage = DefaultLanguage;
                foreach (var item in DictItem.Values)
                {
                    item.LocalItem = null;
                }
            }
            else
            {
                CurrentLanguage = languagePackageName;
                var languagePackage = ResLoadHandler.LoadLanguagePackage(languagePackageName);
                foreach (var localItem in languagePackage.LocalItems)
                {
                    if (DictItem.TryGetValue(localItem.Key, out var item))
                    {
                        item.LocalItem = localItem;
                    }
                }
            }
        }
        public static IEnumerator LoadItemDatabaseAsync(string databaseName)
        {
            bool isLoaded = false;
            ResLoadHandler.LoadItemDatabaseAsync(databaseName, database =>
            {
                foreach (var item in database.Items)
                {
                    AddItemConfig(item);
                }
                isLoaded = true;
            });
            while (!isLoaded) yield return null;
        }
        public static IEnumerator LoadLanguagePackageAsync(string languagePackageName)
        {
            bool isLoaded = false;
            if (languagePackageName == DefaultLanguage)
            {
                CurrentLanguage = DefaultLanguage;
                foreach (var item in DictItem.Values)
                {
                    item.LocalItem = null;
                }
                isLoaded = true;
            }
            else
            {
                CurrentLanguage = languagePackageName;
                ResLoadHandler.LoadLanguagePackageAsync(languagePackageName, languagePackage =>
                {
                    foreach (var localItem in languagePackage.LocalItems)
                    {
                        if (DictItem.TryGetValue(localItem.Key, out var item))
                        {
                            item.LocalItem = localItem;
                        }
                    }
                    isLoaded = true;
                });
            }
            while (!isLoaded) yield return null;
        }
        #endregion

        #region save
        public static void Save() => SaverHandler.Save(DictSlotGroup);
        public static void Load() => SaverHandler.Load(DictSlotGroup);
        public static void Clear() => SaverHandler.Clear();
        #endregion
    }
}
